package com.gec.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.gec.pojo.Article;
import com.gec.pojo.User;
import com.gec.service.ArticleService;
import com.gec.service.UserService;

@Controller("userController")
public class UserController {
	
	@Autowired
	UserService userService;
	@Autowired
	ArticleService as;
	
	@RequestMapping("/logout")
	public String loginOut(HttpServletRequest request,HttpServletResponse response) {
		return "index";
	}
	
	@RequestMapping("/login")
	public String login(String loginName,String password,HttpServletRequest request,HttpServletResponse response) {
		User user = userService.userLogin(loginName,password);
		if(user == null) {
			return "login";
		}
		request.getSession().setAttribute("session_user", user);
		List<Article> aList = as.getArticles();
		request.getSession().setAttribute("aList", aList);
		return "index2";
	}
	
	@RequestMapping("/register")
	public String register(User user) {
		return "register";
	}
	
	@RequestMapping("/regUser")
	public String registerAction(String loginName,String password,String email,HttpServletRequest request,HttpServletResponse response) {
		int a = userService.userRegister(loginName,password,email);
		if(a != 1) {
			return "register";
		}else {
			return "login";
		}
	}
	
	@RequestMapping("/center")
	public String center(HttpServletRequest request,HttpServletResponse response) {
		User user = (User) request.getSession().getAttribute("session_user");
		String loginName = user.getName();
		String password = user.getPassword();
		request.getSession().removeAttribute("session_user");
		user = userService.userLogin(loginName,password);
		request.getSession().setAttribute("session_user", user);
		int lv;
		int expMax = 100;
		for(lv = user.getLv();lv > 0;lv --) {
			expMax *= 2;
		}
		List<Article> aList = as.getArticleByName(user.getName());
		request.getSession().setAttribute("aList", aList);
		request.getSession().setAttribute("expMax", expMax);
		return "personal-data";
	}
	
	@RequestMapping("/change")
	public String change(String name,String email,String sex,int age,int stunum,HttpServletRequest request,HttpServletResponse response) {
		User user = (User) request.getSession().getAttribute("session_user");
		String origin = user.getName();
		userService.update(name,email,sex,age,stunum,origin);
		String loginName = name;
		String password = user.getPassword();
		User newuser = userService.userLogin(loginName,password);
		request.getSession().removeAttribute("session_user");
		request.getSession().setAttribute("session_user", newuser);
		return "personal-data";
	}
	
	@RequestMapping("/data")
	public String data(int id,HttpServletRequest request,HttpServletResponse response) {
		User user = (User) request.getSession().getAttribute("session_user");
		String loginName = user.getName();
		String password = user.getPassword();
		User newuser = userService.userLogin(loginName,password);
		request.getSession().removeAttribute("session_user");
		request.getSession().setAttribute("session_user", newuser);
		return "redirect:look?id=" + id;
	}
}
